package com.zhu.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.model.pojo.Role;
import com.zhu.model.pojo.model.system.SysRole;

import com.zhu.model.pojo.model.system.SysUserRole;
import com.zhu.model.pojo.model.system.vo.AssginRoleVo;
import com.zhu.service.system.mapper.RoleMapper;
import com.zhu.service.system.mapper.UserRoleMapper;
import com.zhu.service.system.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2022-10-28
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, SysRole> implements IRoleService {
    @Resource
    private UserRoleMapper userRoleMapper;

//=============================================角色模块============================================
    @Override
    public IPage<SysRole> selectPage(Page<SysRole> pageParam, String roleName) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getIsDeleted,0);
        if(roleName!=null&&roleName!=""){
            queryWrapper.like(SysRole::getRoleName,roleName);
        }
        IPage<SysRole> rolePage = baseMapper.selectPage(pageParam, queryWrapper);
        return rolePage;
    }

    @Override
    public IPage<SysRole> selectPage2(Page<SysRole> pageParam, String  roleName) {
        IPage<SysRole> rolePage = baseMapper.selectPage2(pageParam,roleName);
        return rolePage;
    }
//======================================角色用户模块========================================================
    @Override
    public Map<String, Object> getRolesByUserId(Long userId) {
        //获取所有角色
        List<SysRole> allRoles = baseMapper.selectList(null);
        //根据用户id查询用户所分配的角色
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId,userId);
        List<SysUserRole> userRoleList = userRoleMapper.selectList(queryWrapper);
        //获取用户所分配的角色id
        List<String>  userRoleIds= userRoleList.stream().map(sysUserRole -> {
                   String roleId=sysUserRole.getRoleId();
                   return roleId;
                }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("allRoles",allRoles);
        map.put("userRoleIds",userRoleIds);
        return map;
    }

    /**
     * 给用户分配角色
     * @param assginRoleVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean assignRole(AssginRoleVo assginRoleVo) {
        //删除用户原本的角色
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId,assginRoleVo.getUserId());
        int delete = userRoleMapper.delete(queryWrapper);
        //重新分配角色
        List<String> roleIdList = assginRoleVo.getRoleIdList();
        ArrayList<Object> list = new ArrayList<>();
        roleIdList.forEach(role->{
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(assginRoleVo.getUserId());
            sysUserRole.setRoleId(role);
            int insert = userRoleMapper.insert(sysUserRole);
            list.add(insert);
        });
        if(delete>0&&list.size()==assginRoleVo.getRoleIdList().size()){
            return true;
        }
        return false;
    }
}
